package com.tcl.aota.admin.manager;

import com.tcl.aota.admin.dto.CurefObj;
import com.tcl.aota.admin.util.CuUtil;
import com.tcl.aota.admin.util.S3Util;
import com.tcl.aota.cache.AppCacheManage;
import com.tcl.aota.common.constants.Constants;
import com.tcl.aota.common.utils.StringUtil;
import com.tcl.aota.manage.CurefManage;
import com.tcl.aota.persistent.model.Curef;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author kelong
 * @date 5/12/15
 */
@Component
public class CurefManager {
    Logger LOG = Logger.getLogger(CurefManager.class);
    @Resource
    private CurefManage curefManage;
    @Resource
    private AppCacheManage appCacheManage;

    /**
     * CU
     */
    public void updateCuref() {
        List<CurefObj> curefs = CuUtil.getCu();
        if (curefs == null || curefs.size() == 0) {
            LOG.info("get CU exception or null");
            return;
        }
        List<Curef> curefList = new ArrayList<>();
        for (CurefObj obj : curefs) {
            Curef curef = new Curef();
            curef.setId(obj.getId());
            curef.setProduct(obj.getProduct());
            curef.setCurefRegexp(obj.getCurefRegexp());
            curef.setCuref(obj.getCuref());
            curef.setCreateUser(obj.getUserName());
            curef.setCreateTime(new Date());
            curefList.add(curef);
        }
        String createUsers = S3Util.getCuRefCreateUser();
        List<String> createUserList = StringUtil.strToList(createUsers);
        try {
            int num = curefManage.updateCuref(curefList, createUserList);
            appCacheManage.delCache(StringUtil.append(Constants.REDIS.CUREF,1));
            appCacheManage.delCache(StringUtil.append(Constants.REDIS.CUREF,0));
            LOG.info("get Cu success," + num);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
